<?php

namespace App\Model;

use App\Comment\Comment;
use Illuminate\Database\Eloquent\Model;

class MallGoods extends Model
{
    public $timestamps = false;
    protected $table = 'mall_goods';

    /**
     * 分页搜索获取菜单的列表
     * @param string $goodsName
     * @param string $sgcId
     * @param string $gtId
     * @return mixed
     */
    public function getGoodsList($goodsName='', $sgcId='', $gtId='', $state = 1)
    {
        $list = $this -> where(['store_id' => Comment::getBizId(), 'goods_state' => $state]);
        if (!empty($goodsName)){
            $list = $list -> where(['goods_name' => $goodsName]);
        }
        if (!empty($sgcId)){
            $list = $list -> where(['mall_goods.sgc_id' => $sgcId]);
        }
        if (!empty($gtId)){
            $list = $list -> where(['mall_goods.gt_id' => $gtId]);
        }
        $list = $list
            -> leftJoin('mall_goods_type', function ($join){
                $join -> on('mall_goods_type.gt_id', '=', 'mall_goods.gt_id');
            })
            -> leftJoin('mall_goods_class', function ($join){
                $join -> on('mall_goods_class.gc_id', '=', 'mall_goods.sgc_id');
            });
        $list = $list -> orderBy('goods_id', 'desc') -> paginate(20) -> toArray();
        return $list;
    }

    /**
     * 修改菜品数据
     * @param int $id
     * @param array $data
     * @return mixed
     */
    public function updateGoods($id, $data)
    {
        return $this -> where(['goods_id' => $id]) -> update($data);
    }

    /**
     * 添加菜品信息
     * @param $data
     * @return mixed
     */
    public function addGoods($data)
    {
        return $this -> insert($data);
    }
}
